<template>
  <div class="activity-stream">
    <div class="activity-header">
      <h3 class="title">最近活动</h3>
      <el-button type="text" size="small" @click="refreshActivities">
        刷新
        <i class="el-icon-refresh"></i>
      </el-button>
    </div>
    
    <div class="activity-content">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :type="getActivityType(activity.type)"
          :color="getActivityColor(activity.type)"
          :timestamp="formatTimestamp(activity.timestamp)"
          :hide-timestamp="false"
          size="small"
        >
          <div class="activity-item">
            <span class="activity-text" v-html="formatActivityText(activity)"></span>
            <span v-if="activity.hash" class="activity-hash">
              <el-tooltip :content="activity.hash" placement="top" effect="light">
                <span class="hash-text">{{ shortenHash(activity.hash) }}</span>
              </el-tooltip>
            </span>
          </div>
        </el-timeline-item>
      </el-timeline>
      
      <div v-if="loading" class="loading-wrapper">
        <el-skeleton :rows="3" animated />
      </div>
      
      <div v-if="!loading && activities.length === 0" class="empty-state">
        <i class="el-icon-document"></i>
        <p>暂无活动记录</p>
      </div>
    </div>
    
    <div class="activity-footer">
      <el-button type="text" @click="loadMore" :disabled="!hasMoreActivities || loading">
        {{ loading ? '加载中...' : '加载更多' }}
      </el-button>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'

dayjs.extend(relativeTime)
dayjs.locale('zh-cn')

export default {
  name: 'ActivityStream',
  props: {
    limit: {
      type: Number,
      default: 5
    }
  },
  setup(props) {
    const activities = ref([])
    const loading = ref(false)
    const hasMoreActivities = ref(true)
    const page = ref(1)
    const pageSize = ref(props.limit || 5)
    
    // 模拟活动数据（实际应用中从API获取）
    const mockActivities = [
      {
        type: 'certificate_issued',
        timestamp: Date.now() - 1000 * 60 * 30, // 30分钟前
        username: '张三',
        certificateId: 'CERT2023001',
        hash: '0xabcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234'
      },
      {
        type: 'student_added',
        timestamp: Date.now() - 1000 * 60 * 60 * 2, // 2小时前
        username: '李四',
        studentId: 'STU20230042'
      },
      {
        type: 'certificate_verified',
        timestamp: Date.now() - 1000 * 60 * 60 * 6, // 6小时前
        certificateId: 'CERT2023005',
        verifier: '王五',
        hash: '0x5678efgh5678efgh5678efgh5678efgh5678efgh5678efgh5678efgh5678efgh'
      },
      {
        type: 'certificate_issued',
        timestamp: Date.now() - 1000 * 60 * 60 * 12, // 12小时前
        username: '赵六',
        certificateId: 'CERT2023002',
        hash: '0xijkl9012ijkl9012ijkl9012ijkl9012ijkl9012ijkl9012ijkl9012ijkl9012'
      },
      {
        type: 'student_added',
        timestamp: Date.now() - 1000 * 60 * 60 * 24, // 1天前
        username: '孙七',
        studentId: 'STU20230043'
      },
      {
        type: 'system_update',
        timestamp: Date.now() - 1000 * 60 * 60 * 36, // 1.5天前
        message: '系统更新：增加批量证书导出功能'
      }
    ]
    
    // 加载活动数据
    const loadActivities = () => {
      loading.value = true
      
      // 模拟API请求延迟
      setTimeout(() => {
        const start = (page.value - 1) * pageSize.value
        const end = start + pageSize.value
        const newActivities = mockActivities.slice(start, end)
        
        activities.value = [...activities.value, ...newActivities]
        
        // 判断是否还有更多数据
        hasMoreActivities.value = activities.value.length < mockActivities.length
        
        loading.value = false
      }, 500)
    }
    
    // 刷新活动列表
    const refreshActivities = () => {
      activities.value = []
      page.value = 1
      hasMoreActivities.value = true
      loadActivities()
    }
    
    // 加载更多活动
    const loadMore = () => {
      if (hasMoreActivities.value && !loading.value) {
        page.value++
        loadActivities()
      }
    }
    
    // 根据活动类型获取图标类型
    const getActivityType = (type) => {
      const typeMap = {
        certificate_issued: 'primary',
        student_added: 'success',
        certificate_verified: 'info',
        system_update: 'warning'
      }
      return typeMap[type] || 'info'
    }
    
    // 根据活动类型获取颜色
    const getActivityColor = (type) => {
      const colorMap = {
        certificate_issued: '#409EFF',
        student_added: '#67C23A',
        certificate_verified: '#909399',
        system_update: '#E6A23C'
      }
      return colorMap[type] || '#909399'
    }
    
    // 格式化时间戳为相对时间
    const formatTimestamp = (timestamp) => {
      return dayjs(timestamp).fromNow()
    }
    
    // 格式化活动文本
    const formatActivityText = (activity) => {
      switch (activity.type) {
        case 'certificate_issued':
          return `<strong>${activity.username}</strong> 获得了证书 <span class="highlight">${activity.certificateId}</span>`
        case 'student_added':
          return `新增学生 <strong>${activity.username}</strong> (${activity.studentId})`
        case 'certificate_verified':
          return `<strong>${activity.verifier || '管理员'}</strong> 验证了证书 <span class="highlight">${activity.certificateId}</span>`
        case 'system_update':
          return `<i class="el-icon-bell"></i> ${activity.message}`
        default:
          return '未知活动'
      }
    }
    
    // 缩短哈希值显示
    const shortenHash = (hash) => {
      if (!hash) return ''
      return `${hash.substring(0, 6)}...${hash.substring(hash.length - 4)}`
    }
    
    onMounted(() => {
      loadActivities()
    })
    
    return {
      activities,
      loading,
      hasMoreActivities,
      refreshActivities,
      loadMore,
      getActivityType,
      getActivityColor,
      formatTimestamp,
      formatActivityText,
      shortenHash
    }
  }
}
</script>

<style scoped>
.activity-stream {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  height: 100%;
  min-height: 450px;
  max-height: 600px;
  display: flex;
  flex-direction: column;
  z-index: 10;
  position: relative;
}

.activity-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.activity-content {
  flex: 1;
  padding: 12px 16px;
  overflow-y: auto;
  max-height: 400px;
  min-height: 300px;
}

.activity-item {
  padding: 4px 0;
  display: flex;
  flex-direction: column;
}

.activity-text {
  font-size: 14px;
  line-height: 1.5;
  color: #303133;
}

.activity-hash {
  margin-top: 4px;
  font-size: 12px;
  color: #909399;
  font-family: monospace;
  display: inline-block;
  background-color: #f5f7fa;
  padding: 2px 8px;
  border-radius: 4px;
  cursor: pointer;
}

.hash-text:hover {
  color: #409EFF;
}

.highlight {
  color: #409EFF;
  font-weight: 500;
}

.loading-wrapper {
  padding: 16px 0;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #909399;
  padding: 40px 0;
}

.empty-state i {
  font-size: 32px;
  margin-bottom: 12px;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

.activity-footer {
  padding: 12px;
  text-align: center;
  border-top: 1px solid #f0f0f0;
}

:deep(.el-timeline-item__tail) {
  left: 5px;
}

:deep(.el-timeline-item__node--small) {
  left: 1px;
}

:deep(.el-timeline-item__content) {
  padding-left: 20px;
}

:deep(.el-timeline-item__timestamp) {
  font-size: 12px;
}
</style> 